//
//  PS_LoginViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/10/24.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_LoginViewController.h"
#import "PS_RegisterViewController.h"
#import "PS_PasswordViewController.h"
#import "ZDXTabBarControllerViewController.h"
//#import "JPUSHService.h"

@interface PS_LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *backScrollVew;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *height_ContantView;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;

@property (strong, nonatomic) IBOutlet UITextField *passwordTF;

@property (strong, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation PS_LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_btmView.hidden = YES;
    self.statusBarView.hidden = YES;
     [_phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
     [_passwordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    //设置placeholder为白色
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:_phoneTF.placeholder];
    [placeholder addAttribute:NSForegroundColorAttributeName
                        value:[UIColor whiteColor]
                        range:NSMakeRange(0, placeholder.length)];
    _phoneTF.attributedPlaceholder = placeholder;
    
    NSMutableAttributedString *placeholder2 = [[NSMutableAttributedString alloc] initWithString:_passwordTF.placeholder];
    [placeholder2 addAttribute:NSForegroundColorAttributeName
                        value:[UIColor whiteColor]
                        range:NSMakeRange(0, placeholder2.length)];
    _passwordTF.attributedPlaceholder = placeholder2;
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)loginAction:(id)sender {
    
    if (_phoneTF.text.length == 0) {

        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];

        return;
    }

    if (_phoneTF.text.length < 11) {

        [MBProgressHUD showError:@"请输入正确手机号码" toView:self.view];

        return;
    }
    if (_passwordTF.text.length == 0) {

        [MBProgressHUD showError:@"请输入密码" toView:self.view];

        return;
    }

    if (_passwordTF.text.length<6 || _passwordTF.text.length>18) {

        [MBProgressHUD showError:@"请输入正确密码" toView:self.view];

        return;
    }
    
    [self goLogin];
    
}


- (void)goLogin{

    [Utils post:LOGIN parameters:@{@"mobile":_phoneTF.text,@"password":_passwordTF.text}  success:^(NSInteger recode, id jsonObject) {

        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        if (dataDic.count > 0) {

            [Utils starageData:dataDic];

            NSString * userid = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"id"]];

//            [JPUSHService setAlias:userid completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {

//            } seq:1];
        }




//        [MobClick event:@"Login"];

        [MBProgressHUD showMessag:[jsonObject objectForKey:@"msg"] toView:self.view];



        [self crearTabBarController];

    } failure:^(NSInteger recode, NSError *error) {

    }];

}


- (IBAction)getNewPasswordAction:(id)sender {
    
    PS_PasswordViewController *VC = [[PS_PasswordViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
- (IBAction)rigisterAction:(id)sender {
    
    PS_RegisterViewController *VC = [[PS_RegisterViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
    
}
-(void)crearTabBarController{
    
    ZDXTabBarControllerViewController * tab = [[ZDXTabBarControllerViewController alloc]init];
    UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
    mainWindow.rootViewController =tab;
//    [mainWindow makeKeyAndVisible];
}

- (void)textFieldDidChange:(UITextField *)textField

{
    
    if (textField == _phoneTF) {
        
        if (textField.text.length > 11) {
            
            textField.text = [textField.text substringToIndex:11];
            
        }
        
//        if (_phoneTF.text.length>10) {
//
//            [Utils post:@"/api/index/checkmobile.html"parameters:@{@"mobile":_phoneTF.text}  success:^(NSInteger recode, id jsonObject) {
//
//            } failure:^(NSInteger recode, NSError *error) {
//
//            }];
//        }
        
    }
    
    if (textField == _passwordTF) {
        
        if (textField.text.length > 18) {
            
            textField.text = [textField.text substringToIndex:18];
            
        }
    }
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
